package com.itheima.pinda.impl.agency;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.DGlobalUser;
import com.itheima.pinda.DTO.entity.dto.PdFleetDTO;
import com.itheima.pinda.DTO.entity.dto.PdTruckDTO;
import com.itheima.pinda.DTO.entity.pojo.agency.PdCoreOrg;
import com.itheima.pinda.DTO.entity.pojo.agency.PdFleet;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.feign.service.DGlobalUserService;
import com.itheima.pinda.feign.service.agency.PdCoreOrgService;
import com.itheima.pinda.feign.service.agency.PdFleetService;
import com.itheima.pinda.feign.service.truck.PdTruckService;
import com.itheima.pinda.feign.service.user.PdTruckDriverService;
import com.itheima.pinda.mapper.agency.PdFleetMapper;
import com.itheima.pinda.utils.CustomIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车队表 服务实现类
 * </p>
 *
 * @author xy
 * @since 2021-11-12
 */
@Service
public class PdFleetServiceImpl extends ServiceImpl<PdFleetMapper, PdFleet> implements PdFleetService {

    @Autowired
    private CustomIdGenerator customIdGenerator;

    @Autowired
    private PdTruckService truckService;

    @Autowired
    private PdTruckDriverService truckDriverService;

    @Autowired
    private DGlobalUserService globalUserService;


    @Autowired
    private PdCoreOrgService coreOrgService;

    @Override
    public List<PdFleetDTO> findAllFleet(String name , String fleetNum,String manager) {
        QueryWrapper<PdFleet> wrapper = new QueryWrapper<>();
        wrapper.eq("status", Constant.DATA_DEFAULT_STATUS);
        if (!ObjectUtils.isEmpty(name)){
            wrapper.eq("name",name);
        }
        if (!ObjectUtils.isEmpty(fleetNum)){
            wrapper.eq("fleet_num",fleetNum);
        }
        if (!ObjectUtils.isEmpty(manager)){
            wrapper.eq("manager",manager);
        }
        List<PdFleet> fleetList = super.baseMapper.selectList(wrapper);
//        将pdFleet对象复制到对象PdFleetDTO里面去
        List<PdFleetDTO> dtoList = new ArrayList<>();



        for ( PdFleet fleet: fleetList ) {

            PdFleetDTO dto = new PdFleetDTO();
            BeanUtils.copyProperties(fleet,dto);
//          查询出该车队的司机总数
            Integer count = truckDriverService.findByFleet(fleet.getId());
//            int driverCount = drivers.size();


//            查找机构名称
            QueryWrapper<PdCoreOrg> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",fleet.getAgencyId());
            PdCoreOrg pdCoreOrg = coreOrgService.getBaseMapper().selectOne(queryWrapper);

            //            查找机构名称
            QueryWrapper<DGlobalUser> query = new QueryWrapper<>();
            query.eq("id",fleet.getManager());
            DGlobalUser dGlobalUser = globalUserService.getBaseMapper().selectOne(query);


//          查询出该车队的车辆总数
            List<PdTruckDTO> trucks = truckService.findByFleet(fleet.getId());
            int truckCount = trucks.size();

//            System.out.println(addressBook);

            if (!StringUtils.isEmpty(dGlobalUser.getMobile())){
                dto.setLink(dGlobalUser.getMobile());
            }
            dto.setAgency(pdCoreOrg.getName());
            dto.setCountDriver(count);
            dto.setCountCar(truckCount);
            dto.setManagerName(dGlobalUser.getName());

            dtoList.add(dto);

        }

        return dtoList;
    }

    @Override
    public boolean deleteUpd(String id) {
        if (ObjectUtils.isEmpty(id)){
            throw  new NullPointerException("你输入的车队编号为空");
        }
        PdFleet fleet = new PdFleet();
        fleet.setId(id);
        fleet.setStatus(0);

        int i = super.baseMapper.updateById(fleet);

        return i != 0;
    }

    @Override
    public boolean saveFleet(String name, String managerId,String agencyId ,String phone) {

        PdFleet fleet = new PdFleet();



        QueryWrapper<DGlobalUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id",managerId);
        DGlobalUser globalUser = globalUserService.getBaseMapper().selectOne(wrapper);





        fleet.setName(name);
        fleet.setManager(globalUser.getName());
        fleet.setAgencyId(agencyId);
        fleet.setId(customIdGenerator.nextId(fleet)+"");
        fleet.setFleetNumber(customIdGenerator.nextId(fleet)+"");
        fleet.setStatus(Constant.DATA_DEFAULT_STATUS);


        int insert = super.baseMapper.insert(fleet);
        return insert != 0;
    }


    @Override
    public List<PdFleetDTO> findAdvance(String name, String fleetNum, String manager) {

        QueryWrapper<PdFleet> wrapper = new QueryWrapper<>();

        wrapper.eq("status", Constant.DATA_DEFAULT_STATUS);

        if (!ObjectUtils.isEmpty(fleetNum)){
            wrapper.like("fleet_num",fleetNum);
        }
        if (!ObjectUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!ObjectUtils.isEmpty(manager)){
            wrapper.eq("manager",manager);
        }

        List<PdFleet> fleetList = super.baseMapper.selectList(wrapper);
//        将pdFleet对象复制到对象PdFleetDTO里面去
        List<PdFleetDTO> dtoList = new ArrayList<>();
        for ( PdFleet fleet: fleetList ) {

            PdFleetDTO dto = new PdFleetDTO();
            BeanUtils.copyProperties(fleet,dto);
//          查询出该车队的司机总数
            Integer count = truckDriverService.findByFleet(fleet.getId());
//            int driverCount = drivers.size();

//          查询出该车队的车辆总数
            List<PdTruckDTO> trucks = truckService.findByFleet(fleet.getId());
            int truckCount = trucks.size();

            dto.setCountDriver(count);
            dto.setCountCar(truckCount);

            dtoList.add(dto);

        }

        return dtoList;
    }


}
